package com.gr3g.dev.draw;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import com.gr3g.dev.learndraw.model.Dessin;

/** @author Gregory Tardivel */
public final class TreeDraw extends JTree {

    /** Numéro de série. */
    private static final long serialVersionUID = 3176705138490975468L;

    private JPopupMenu menu;
    private JMenuItem deleteNode;

    /** Constructeur. */
    public TreeDraw() {
        this.addMouseListener(this.gererMenu);
    }

    public void events() {
        this.menu = new JPopupMenu();

        final JMenuItem addCircle = new JMenuItem("Ajouter cercle");
        addCircle.addActionListener(this.actionAddCircle);

        final JMenuItem addArc = new JMenuItem("Ajouter arc");
        addArc.addActionListener(this.actionAddArc);

        final JMenuItem addNodeLine = new JMenuItem("Ajouter ligne");
        addNodeLine.addActionListener(this.actionAddMultiLine);

        this.menu.add(addNodeLine);
        this.menu.add(addCircle);
        this.menu.add(addArc);

        this.menu.addSeparator();

        final JMenuItem edit = new JMenuItem("Editer");
        edit.addActionListener(this.actionEdit);
        this.menu.add(edit);

        this.menu.addSeparator();

        this.deleteNode = new JMenuItem("Supprimer élément");
        this.deleteNode.addActionListener(this.actionDelete);
        this.deleteNode.setEnabled(false);
        this.menu.add(this.deleteNode);

        this.menu.addSeparator();

        final JMenuItem export = new JMenuItem("Exporter");
        export.addActionListener(this.actionExporter);
        this.menu.add(export);
    }

    private ActionListener actionOpen;
    private ActionListener actionAddCircle;
    private ActionListener actionAddArc;
    private ActionListener actionAddMultiLine;
    private ActionListener actionDelete;
    private ActionListener actionExporter;
    private ActionListener actionEdit;

    /** @param pActionOpen the actionOpen to set */
    public void setActionOpen(final ActionListener pActionOpen) {
        this.actionOpen = pActionOpen;
    }

    /** @param pActionEdit the actionEdit to set */
    public void setActionEdit(final ActionListener pActionEdit) {
        this.actionEdit = pActionEdit;
    }

    /** @param pActionAddArc the actionAddArc to set */
    public void setActionAddArc(final ActionListener pActionAddArc) {
        this.actionAddArc = pActionAddArc;
    }

    /** @param pActionExporter the actionExporter to set */
    public void setActionExporter(final ActionListener pActionExporter) {
        this.actionExporter = pActionExporter;
    }

    /** @param pActionAddCircle the actionAddCircle to set */
    public void setActionAddCircle(final ActionListener pActionAddCircle) {
        this.actionAddCircle = pActionAddCircle;
    }

    /** @param pActionAddMultiLine the actionAddMultiLine to set */
    public void setActionAddMultiLine(final ActionListener pActionAddMultiLine) {
        this.actionAddMultiLine = pActionAddMultiLine;
    }

    /** @param pActionDelete the actionDelete to set */
    public void setActionDelete(final ActionListener pActionDelete) {
        this.actionDelete = pActionDelete;
    }

    private final MouseAdapter gererMenu = new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent pEvent) {
            if (pEvent.getButton() == MouseEvent.BUTTON3) {
                final int rowSelected = TreeDraw.this.getRowForLocation(pEvent
                        .getX(), pEvent.getY());
                TreeDraw.this.menu.show((Component) pEvent.getSource(), pEvent
                        .getX(), pEvent.getY());

                TreeDraw.this.setSelectionRow(rowSelected);
                TreeDraw.this.deleteNode.setEnabled(rowSelected > 0);
                if (TreeDraw.this.getSelected() instanceof Dessin
                        || rowSelected == 0) {
                    boolean exist = false;
                    for (final Component comp : TreeDraw.this.menu
                            .getComponents()) {
                        if ("open".equals(comp.getName())) {
                            exist = true;
                            break;
                        }
                    }
                    if (!exist) {
                        final JMenuItem open = new JMenuItem("Ouvrir");
                        open.setName("open");
                        open.addActionListener(TreeDraw.this.actionOpen);
                        TreeDraw.this.menu.add(open, 0);
                    }
                } else {
                    for (final Component comp : TreeDraw.this.menu
                            .getComponents()) {
                        if ("open".equals(comp.getName())) {
                            TreeDraw.this.menu.remove(comp);
                        }
                    }
                }
                TreeDraw.this.menu.updateUI();
            }
        }

    };

    public Object getSelected() {
        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) this
                .getSelectionPath().getLastPathComponent();
        return node.getUserObject();
    }

}
